package Main;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import AIproccess.AIManager;
import AIproccess.AIManagerAmater;
import AIproccess.AIManagerNoob;
import AIproccess.AIManagerPro;
import Army.ComputerTeam;
import Army.PlayerTeam;
import Component.MyForm;
import GameObject.LayerManager;
import GameObject.Map;
import Global.GlobalGameOption;
import Global.ResourceManager;
import MOM.Center;
import MOM.Topic;
import java.awt.Point;
import java.awt.Rectangle;

/**
 *
 * @author Huy
 */
public class GameLoadingState extends GameState {

	static final int[][] terrainParameter = new int[][]{
		{15, 4, 11},
		{11, 1, 9},
		{5, 0, 7}};
	GameCustumSetting gameSetting;

	public GameLoadingState(Game _game, GameCustumSetting setting) {
		super(_game);
		gameSetting = setting;
		form = new MyForm(ResourceManager.getImage("menu/loadingbackground"), game.getWidth(), game.getHeight());
		GlobalGameOption.MUSIC.getMusic("menu").stop();
	}
	boolean flag = false;

	@Override
	public void update(float t) {
		if (!flag) {
			Center.topics.clear();
			try {
				Global.ResourceManager.InitArmy();
			} catch (Exception ex) {
			}
			int n = 0;
			for (int i = 0; i < gameSetting.playerList.length; i++) {
				if (gameSetting.playerList[i] < 2) {
					n++;
				}
			}
			int size = gameSetting.mapSize * 25 + 50;

			Point[] points = new Point[n];
			for (int i = 0; i < n; i++) {
				int x = (int) (size / 2 + Math.cos(0.5 + i * 2 * Math.PI / n) * (size / 3 + Math.random() * size / 10));
				int y = (int) (size / 2 + Math.sin(0.5 + i * 2 * Math.PI / n) * (size / 3 + Math.random() * size / 10));
				points[i] = new Point(x, y);
			}

			int tIndex = gameSetting.mapType;

			Map map = new Map(ResourceManager.getImage("ingame/map"), 6, 2, Map.createMatrix(size, size, terrainParameter[tIndex][0] * (1 + size / 100) + (size / (n * n)) / 2, terrainParameter[tIndex][1], terrainParameter[tIndex][2], points), 3);
			LayerManager layerManager = new LayerManager(
					game.getHeight(), game.getHeight(), n);
			layerManager.setLimitBound(new Rectangle(map.getX(), map.getY(), map.getWidth(), map.getHeight()));
			layerManager.setMap(map);
			PlayerTeam.LIMIT = gameSetting.limit;
			Topic myTopic = null;
			PlayerTeam myTeam = null;
			ComputerTeam[] comTeams = new ComputerTeam[n-1];
			int k = 0;

			for (int i = 0; i < gameSetting.playerList.length; i++) {
				int l = gameSetting.playerList[i];

				if (l == GameCustomState.PLAYER) {
					int pi = Game.RANDOM.nextInt(n--);
					myTeam = new PlayerTeam(i, points[pi].x * map.getCellWidth(), points[pi].y * map.getCellHeight());
					/*myTeam.createNewUnit("worker", points[0].x * map.getCellWidth(), points[0].y * map.getCellHeight());*/
					myTeam.setGold(gameSetting.startMoney);

					myTopic = new Topic(i);
					Center.topics.add(myTopic);
					Center.subcribe(i, myTeam);

					
					layerManager.setTeam(0, myTeam);
					layerManager.move(points[pi].x * map.getCellWidth() - 300, points[pi].y * map.getCellHeight() - 300);
					points[pi].x=points[n].x;
					points[pi].y=points[n].y;
				} else if (l == GameCustomState.COMPUTER) {
					int pi = Game.RANDOM.nextInt(n--);
					comTeams[k] = new ComputerTeam(i, points[pi].x * map.getCellWidth(), points[pi].y * map.getCellHeight());
					//points[pi]=points[n];
					points[pi].x=points[n].x;
					points[pi].y=points[n].y;
					/*comTeams[k].createNewUnit("worker", points[k + 1].x * map.getCellWidth(), points[k + 1].y * map.getCellHeight());*/
					comTeams[k].setGold(gameSetting.startMoney);
					Topic topic = new Topic(i);
					Center.topics.add(topic);
					Center.subcribe(i, comTeams[k]);
					layerManager.setTeam(k + 1, comTeams[k]);
					k++;
				}

			}
			AIManager ai;
			if (gameSetting.level == 0) {
				ai = new AIManagerNoob(layerManager);
			} else if (gameSetting.level == 1) {
				ai = new AIManagerAmater(layerManager);
			} else {
				ai = new AIManagerPro(layerManager);
			}
			GamePlayState gameState = new GamePlayState(game
					, layerManager, myTeam, comTeams, ai, gameSetting.allowCheat);
			if (myTopic != null) {
				myTopic.subcribe(gameState);
			}
			game.gameState = gameState;
			flag = true;
		}
	}
}
